
#import "iPhoneOSBuild.h"

#if USE_UIKIT

#import <Foundation/Foundation.h>

#include <stdio.h>
#include <stdint.h>
#include <pthread.h>

extern  int     myargc;
extern  char**  myargv;

extern void updateScreen();
extern void D_DoomMain();

void iPhone_Video_End()
{
}

void iPhone_Video_LoadFrame(unsigned long *fb)
{
	setFB(fb);
	updateScreen();
}

void iPhone_Video_Init()
{
}

void *game_thread(void *args) {
	myargc = 1;    
	myargv = (char**) malloc (sizeof(*myargv) * (myargc+3));
	
	if (myargv == NULL) 
	{
		return NULL;
	}
	
	myargv[0] = (char*)malloc(strlen("/Applications/Doom.app/doom") + 1);
	sprintf(myargv[0], "/Applications/Doom.app/doom");
	myargv[1] = (char*)malloc(strlen("-episode") + 1);
	sprintf(myargv[1], "-episode");
	myargv[2] = (char*)malloc(strlen("0") + 1);
	sprintf(myargv[2], "0");
	
	myargv[myargc + 2] = NULL;
	
	myargc += 2;	
	
	NSAutoreleasePool * pool = [NSAutoreleasePool new];
	setenv("DOOMWADDIR", [[[NSBundle mainBundle] resourcePath] UTF8String], 1);
	[pool release];	
	
	D_DoomMain ();
	
	if(myargv)
	{
		int i;
		for(i = 0; i < myargc; i++)
		{
			if(myargv[i] != NULL) free(myargv[i]);
		}
		free(myargv);
		myargv = NULL;
	}
	
	return NULL;
}

#endif
